<template>
    <div>
        <el-card>
            <el-table border style="margin: 10px 0;" :data="skuArr">
                <el-table-column label="序号" type="index" align="center" width="80" />
                <el-table-column label="名称" show-overflow-tooltip width="200px" prop="skuName" />
                <el-table-column label="描述" show-overflow-tooltip width="300px" prop="skuDesc" />
                <el-table-column label="图片" width="150px">
                    <template #default="scope">
                        <img :src="scope.row.skuDefaultImg" alt="暂无图片" style="width: 100px; height: 100px">
                    </template>
                </el-table-column>
                <el-table-column label="重量" width="150px" prop="weight" />
                <el-table-column label="价格" width="150px" prop="price" />
                <el-table-column label="操作" width="250px" fixed="right">
                    <template #="{ row }">
                        <el-button :icon="row.isSale == 1 ? 'Bottom' : 'Top'" size="small" type="primary"
                            @click="updateSale(row)"></el-button>
                        <el-button icon="Edit" size="small" type="primary" @click="updateSku"></el-button>
                        <el-button icon="InfoFilled" size="small" type="primary" @click="findSku(row)"></el-button>
                        <el-popconfirm :title="`你确定要删除${row.skuName}?`" width="200px" @confirm="removeSku(row.id)">
                            <template #reference>
                                <el-button icon="Delete" size="small" type="primary"></el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :background="true"
                :page-sizes="[10, 20, 30, 40]" :total="total" layout="prev, pager, next, jumper,->,sizes,total"
                @current-change="getHasSku" @size-change="handler" />
            <!--抽屉组件：展示商品详情-->
            <el-drawer v-model="drawer" :direction="direction">
                <template #header>
                    <h4>查看商品详情</h4>
                </template>
                <template #default>
                    <el-row>
                        <el-col :span="6">名称</el-col>
                        <el-col :span="18">{{ skuInfo.skuName }}</el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">描述</el-col>
                        <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">价格</el-col>
                        <el-col :span="18">{{ skuInfo.price }}</el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">平台属性</el-col>
                        <el-col :span="18">
                            <el-tag :style="{ marginRight: '6px' }" v-for="item in skuInfo.skuAttrValueList" :key="item.id">{{ item.valueName }}</el-tag>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">销售属性</el-col>
                        <el-col :span="18">
                            <el-tag :style="{ marginRight: '6px' }" v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id">{{ item.saleAttrValueName }}</el-tag>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">商品图片</el-col>
                        <el-col :span="18">
                            <el-carousel :interval="4000" type="card" height="200px">
                                <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                                    <img :src="item.imgUrl" alt="" style="width: 100%; height: 100;">
                                </el-carousel-item>
                            </el-carousel>
                        </el-col>
                    </el-row>
                </template>
            </el-drawer>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { reqCancelSale, reqSaleSku, reqSkuList, reqSkuInfo, reqRemoveSku } from '@/api/product/sku';
import type { SkuResponseData } from '@/api/product/sku/type';
import type { SkuData } from '@/api/product/sku/type';
import { ElMessage } from 'element-plus';
import { onMounted, ref } from 'vue';

let pageNo = ref<number>(1)
let pageSize = ref<number>(10)
let total = ref<number>(0)
let skuArr = ref<SkuData[]>([])
let drawer = ref(false)
let direction = ref<'rtl' | 'ltr' | 'ttb' | 'btt'>('rtl')
let skuInfo = ref<SkuData>({})
//组件挂载完毕
onMounted(() => {
    getHasSku()
})

//获取已有的sku数据
const getHasSku = async (pager = 1) => {
    pageNo.value = pager
    let result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)
    if (result.code === 200) {
        skuArr.value = result.data.records
        total.value = result.data.total
    }
}

const handler = () => {
    getHasSku()
}

const updateSale = async (row: SkuData) => {
    //如果isSale==1，就是上架状态，需要下架，反之亦然
    if (row.isSale == 1) {
        let result = await reqCancelSale(row.id as number)
        if (result.code == 200) {
            ElMessage.success('下架成功')
            getHasSku(pageNo.value)
        } else {
            ElMessage.error(result.message)
        }
    } else {
        let result = await reqSaleSku(row.id as number)
        if (result.code == 200) {
            ElMessage.success('上架成功')
            getHasSku(pageNo.value)
        } else {
            ElMessage.error(result.message)
        }
    }
}

const updateSku = () => {
    ElMessage.success('程序员在努力的更新中，敬请期待')
}

const findSku = (row: SkuData) => {
    //抽屉展示出来
    drawer.value = true
    //获取sku信息
    reqSkuInfo(row.id as number).then((result) => {
        if (result.code === 200) {
            // console.log(result.data)
            skuInfo.value = result.data
        }else{
            ElMessage.error(result.message)
        }
    })

}
//删除某个已有商品
const removeSku = (id: number) => {
    reqRemoveSku(id).then((result) => {
        if (result.code === 200) {
            ElMessage.success('删除成功')
            getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
        } else {
            ElMessage.error(result.message)
        }
    })
}
</script>

<style scoped>
.el-carousel__item h3 {
    color: #475669;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
    text-align: center;
}

.el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
    background-color: #d3dce6;
}
</style>